package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_resend_partner;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_resend_partner;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_resend_partnerSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_resend_partnerService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[业务伙伴需要额外信息重发EMail] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_resend_partnerServiceImpl implements IMail_resend_partnerService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_resend_partnerClientService mail_resend_partnerClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_resend_partner clientModel = mail_resend_partnerClientService.createModel();
        clientModel.setId(id);
        mail_resend_partnerClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mail_resend_partner get(Integer id) {
        Imail_resend_partner clientModel = mail_resend_partnerClientService.createModel();
        clientModel.setId(id);
        mail_resend_partnerClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_resend_partner get(Mail_resend_partner mail_resend_partner) {
        Mail_resend_partner retmail_resend_partner = this.get(mail_resend_partner.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_resend_partner.getClass(), mail_resend_partner.getClass(), false);
        copier.copy(retmail_resend_partner, mail_resend_partner, null);
        return mail_resend_partner ;
    }

    @Override
    public void createBatch(List<Mail_resend_partner> mail_resend_partners, int batchSize) {

                        //mail_resend_partnerClientService.createBatch(mail_resend_partners,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_resend_partnerClientService.removeBatch(mail_resend_partners) ;

    }

    @Override
    public boolean create(Mail_resend_partner mail_resend_partner) {
        boolean bOk = false ;
        Imail_resend_partner clientModel = this.convert2Model(mail_resend_partner,null);
        mail_resend_partnerClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_resend_partner);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mail_resend_partner mail_resend_partner) {
        boolean bOk = false ;
        Imail_resend_partner clientModel = this.convert2Model(mail_resend_partner,null);
        mail_resend_partnerClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_resend_partner) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Mail_resend_partner> mail_resend_partners, int batchSize) {

        //mail_resend_partnerClientService.updateBatch(mail_resend_partners,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_resend_partner> searchDefault(Mail_resend_partnerSearchContext context) {
        List<Mail_resend_partner> list = new ArrayList<Mail_resend_partner>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_resend_partner> clientModelList = mail_resend_partnerClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_resend_partner clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_resend_partner>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail重发向导查询
     */
    @Override
    public Page<Mail_resend_partner> selectByOdoo_resend_wizard(Integer id){
        Mail_resend_partnerSearchContext context = new Mail_resend_partnerSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_resend_partner> list = new ArrayList<Mail_resend_partner>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_resend_partner> clientModelList = mail_resend_partnerClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_resend_partner clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_resend_partner>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Mail_resend_partner> selectByOdoo_partner(Integer id){
        Mail_resend_partnerSearchContext context = new Mail_resend_partnerSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_resend_partner> list = new ArrayList<Mail_resend_partner>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_resend_partner> clientModelList = mail_resend_partnerClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_resend_partner clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_resend_partner>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_resend_partner> selectByOdoo_create(Integer id){
        Mail_resend_partnerSearchContext context = new Mail_resend_partnerSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_resend_partner> list = new ArrayList<Mail_resend_partner>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_resend_partner> clientModelList = mail_resend_partnerClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_resend_partner clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_resend_partner>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_resend_partner> selectByOdoo_write(Integer id){
        Mail_resend_partnerSearchContext context = new Mail_resend_partnerSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_resend_partner> list = new ArrayList<Mail_resend_partner>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_resend_partner> clientModelList = mail_resend_partnerClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_resend_partner clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_resend_partner>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_resend_partner convert2Model(Mail_resend_partner domain , Imail_resend_partner model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_resend_partnerClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getResendDirtyFlag())
            model.setResend(domain.getResend());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getMessageDirtyFlag())
            model.setMessage(domain.getMessage());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getEmailDirtyFlag())
            model.setEmail(domain.getEmail());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getResend_wizard_idDirtyFlag())
            model.setResend_wizard_id(domain.getResend_wizard_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Mail_resend_partner convert2Domain( Imail_resend_partner model ,Mail_resend_partner domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_resend_partner();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getResendDirtyFlag())
            domain.setResend(model.getResend());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getMessageDirtyFlag())
            domain.setMessage(model.getMessage());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getEmailDirtyFlag())
            domain.setEmail(model.getEmail());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getResend_wizard_idDirtyFlag())
            domain.setResend_wizard_id(model.getResend_wizard_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
